﻿using System;
using System.Linq;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class AddEducationPage : System.Web.UI.Page
    {
        private Applicant _currentApplicant; 

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentApplicant = Session["CurrentUser"] as Applicant;

            if (!IsPostBack)
            {
                FillDropDownLists();
            }
        }
        protected void ButtonAddNewEducation_Click(object sender, EventArgs e)
        {
            AddNewEducation();
        }
      
        protected void ButtonAddNewWorkExp_OnClick(object sender, EventArgs ev)
        {
            AddNewWorkExperience();
        }
     
        protected void ButtonAddNewMerit_OnClick(object sender, EventArgs e)
        {
            AddNewMerit();
        }

        private void AddNewMerit()
        {
            var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

            var userMerits =
                currentUserCv.Merits.Where(m => m.CvCvId == currentUserCv.CvId).ToList();

            var newMerit = new Merits
            {
                Desription = TextBoxAddMerit.Text,
                CvCvId = currentUserCv.CvId,
                Ranking = (RankingMerits) Enum.Parse(typeof (RankingMerits), DropDownListRankMerit.SelectedValue)
            };

            userMerits.Add(newMerit);
            CvDB.CreateMerit(userMerits.Last());
            TextBoxAddMerit.Text = "";
        }
        private void AddNewEducation()
        {
            var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

            var userEducations =
                currentUserCv.Educations.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

            var newEducation = new Educations
            {
                Desription = TextBoxAddEducation.Text,
                CvCvId = currentUserCv.CvId
            };

            userEducations.Add(newEducation);

            CvDB.CreateEducations(userEducations.Last());
            TextBoxAddEducation.Text = "";
        }
        private void AddNewWorkExperience()
        {
            var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

            var userWorkExperiences =
                currentUserCv.Experiences.Where(e => e.CvCvId == currentUserCv.CvId).ToList();

            var newWorkExp = new Experiences
            {
                Description = TextBoxAddWorkExp.Text,
                CvCvId = currentUserCv.CvId,
                Ranking = (Ranking)Enum.Parse(typeof(Ranking), DropDownListrankWorkExp.SelectedValue)
            };

            userWorkExperiences.Add(newWorkExp);

            CvDB.CreateExperiences(userWorkExperiences.Last());
            TextBoxAddWorkExp.Text = "";
        }
        private void FillDropDownLists()
        {
            DropDownListRankMerit.DataSource = Enum.GetNames(typeof(RankingMerits));
            DropDownListRankMerit.DataBind();

            DropDownListrankWorkExp.DataSource = Enum.GetNames(typeof(Ranking));
            DropDownListrankWorkExp.DataBind();
        }
    }
}